package com.example.reception.dao;

import com.example.reception.entity.Products;
import com.example.system.model.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @author: adnaan
 * @create: 2023-10-20 10:07
 * @Description:
 */
@Component
public class ProductsDao {
    @Resource
    private MongoTemplate mongoTemplate;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public List<Products> get(Query query) {
        return mongoTemplate.find(query, Products.class);
    }

    public ApiResponse findPaginated(int pageNum, int pageSize, String name) {
        String reges = String.format("%s%s%s", "^.*", name, ".*$");//匹配规则

        Pattern pattern = Pattern.compile(reges, Pattern.CASE_INSENSITIVE);//正则匹配规则--pattern的常量
        Query query = new Query(Criteria.where("name").regex(pattern));
        //查询集合记录总数
        long total = mongoTemplate.count(query, Products.class);
        //分页查询
        List<Products> comments = mongoTemplate.find(query.skip((pageNum - 1) * pageSize).limit(pageSize), Products.class);
        //条件体
        Map<String, Object> res = new HashMap<>();
        res.put("total", total);
        res.put("data", comments);

        return ApiResponse.success(res);
    }

    public Products findDetail(String id) {
        return mongoTemplate.findById(id, Products.class);
    }

    public ApiResponse addProduct(Products products) {
        return ApiResponse.success(mongoTemplate.save(products));
    }

    public ApiResponse delete(Query query) {
        return ApiResponse.success(mongoTemplate.remove(query, Products.class));
    }

    public ApiResponse update(Query query, Update update) {
        return ApiResponse.success(mongoTemplate.updateMulti(query, update, Products.class));
    }

    public ApiResponse hotGet() {
        return ApiResponse.success(stringRedisTemplate.opsForZSet().reverseRangeByScore("product", 1, Double.POSITIVE_INFINITY));
    }

    public ApiResponse redisHashAdd(String shopping_redis, String productId, Integer quantity) {
        HashOperations<String,String,String> hashOperations = stringRedisTemplate.opsForHash();
        if(quantity==0){
            hashOperations.delete(shopping_redis,productId);
        }else{
            hashOperations.put(shopping_redis, productId, String.valueOf(quantity));
        }
        return ApiResponse.success();
    }

    public ApiResponse redisHashClear(String shopping_redis,String productId) {
        HashOperations<String,String,String> hashOperations = stringRedisTemplate.opsForHash();
        String currentQuantityString = hashOperations.get(shopping_redis,productId);
        int currentQuantity = (currentQuantityString != null) ? Integer.parseInt(currentQuantityString) : 0;
        if(currentQuantity!=0){
            hashOperations.delete(shopping_redis,productId);
            return ApiResponse.success();
        }
        return ApiResponse.error();
    }

    public ApiResponse redisHashGet(String shoppingRedis) {
        HashOperations<String,String,String> hashOperations = stringRedisTemplate.opsForHash();
        Map<String,String> cartContents = hashOperations.entries(shoppingRedis);

        Map<String,Integer> result = new HashMap<>();
        for(Map.Entry<String,String> entry : cartContents.entrySet()){
            result.put(entry.getKey(), Integer.parseInt(entry.getValue()));
        }
        return ApiResponse.success(result);
    }
}
